﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import Engine.Interfaces.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.*;

	/**
	 * 用大位图的方式实现小人的动画
	 */
    public class AnimatedBitmap extends Bitmap implements IDynamicDisplayObject {

        public static const ANIMATE_FORWARD:int = 1;
        public static const ANIMATE_REVERSE:int = -1;

        protected static var m_destPoint:Point = new Point(0, 0);

        protected var m_targetBitmapData:BitmapData;
        private var m_spriteSheet:BitmapData;
        private var m_numFrames:int;
        private var m_frameDelay:Number;
        protected var m_frameWidth:int;
        protected var m_frameHeight:int;
        protected var m_frameRect:Rectangle;
        private var m_currentFrame:int;
        private var m_frameTimer:Number = 0;
        private var m_isPlaying:Boolean = true;
        private var m_direction:int;

        public function AnimatedBitmap(_arg1:BitmapData, _arg2:int, _arg3:int, _arg4:int, _arg5:Number){
            this.m_targetBitmapData = new BitmapData(_arg3, _arg4, true, 0);
            this.m_spriteSheet = _arg1;
            this.m_frameWidth = _arg3;
            this.m_frameHeight = _arg4;
            this.m_frameDelay = (1000 / _arg5);
            this.m_numFrames = _arg2;
            this.m_frameRect = new Rectangle(0, 0, _arg3, _arg4);
            this.m_currentFrame = 0;
            this.m_direction = ANIMATE_FORWARD;
            super(this.m_targetBitmapData, PixelSnapping.NEVER);
            this.draw();
        }
        public function reset():void{
            this.m_currentFrame = 0;
            this.m_isPlaying = true;
        }
        public function randomFrame():void{
            this.m_currentFrame = MathUtil.random((this.m_numFrames - 1));
        }
        public function get numFrames():Number{
            return (this.m_numFrames);
        }
        public function get currentFrame():Number{
            return (this.m_currentFrame);
        }
        public function set currentFrame(_arg1:Number):void{
            this.m_currentFrame = MathUtil.clamp(_arg1, 0, this.m_numFrames);
        }
        public function get isPlaying():Boolean{
            return (Boolean(this.m_isPlaying));
        }
        public function get targetBitmapData():BitmapData{
            return (this.m_targetBitmapData);
        }
        public function set targetBitmapData(_arg1:BitmapData):void{
            this.m_targetBitmapData = _arg1;
        }
        public function get direction():int{
            return (this.m_direction);
        }
        public function set direction(_arg1:int):void{
            this.m_direction = _arg1;
        }
        public function onUpdate(_arg1:Number):void{
            var _local2:Number;
            var _local3:int;
            if (this.m_isPlaying){
                this.m_frameTimer = (this.m_frameTimer + (_arg1 * 1000));
                _local2 = Math.floor((this.m_frameTimer / this.m_frameDelay));
                if (_local2 > 0){
                    _local3 = this.m_currentFrame;
                    this.m_currentFrame = (this.m_currentFrame + (this.m_direction * _local2));
                    if (this.m_currentFrame >= this.m_numFrames){
                        this.m_currentFrame = (this.m_currentFrame % this.m_numFrames);
                    } else {
                        if (this.m_currentFrame < 0){
                            this.m_currentFrame = (this.m_numFrames - ((-1 * this.m_currentFrame) % this.m_numFrames));
                        };
                    };
                    this.m_frameTimer = (this.m_frameTimer - (_local2 * this.m_frameDelay));
                    if (this.m_currentFrame != _local3){
                        this.draw();
                    };
                };
            } else {
                this.m_frameTimer = 0;
            };
        }
        public function stop():void{
            this.m_isPlaying = false;
        }
        public function play():void{
            this.m_isPlaying = true;
        }
        protected function draw():void{
            var _local1:int = Math.floor((this.m_spriteSheet.width / this.m_frameWidth));
            this.m_frameRect.x = ((this.m_currentFrame % _local1) * this.m_frameWidth);
            this.m_frameRect.y = (Math.floor((this.m_currentFrame / _local1)) * this.m_frameHeight);
            this.m_targetBitmapData.copyPixels(this.m_spriteSheet, this.m_frameRect, m_destPoint);
        }
        public function get spriteSheet():BitmapData{
            return (this.m_spriteSheet);
        }
        public function set spriteSheet(_arg1:BitmapData):void{
            this.m_spriteSheet = _arg1;
        }

    }
}//package Engine.Classes 
